#include<iostream>
#include<string>
#include<vector>
#include<fstream>

unsigned int num(1),count(0);
constexpr unsigned int size = 200;

typedef struct record {
	unsigned int no;
	std::string phone;
	std::string name;
	std::string address;
}record;

class telebook {
private:
	std::vector<record> t_list;
public:
	telebook() = default;
	~telebook() = default;
	void input(const std::string &p, const std::string &n, const std::string &a);
	void output();
	void del(const unsigned int no);
	void search_name(const std::string& name) const;
	void search_phone(const std::string& phone) const;
	void edit(unsigned int no);
};

void telebook::input(const std::string &p, const std::string &n, const std::string &a) {
	record tmp;
	tmp.phone = p;
	tmp.name = n;
	tmp.address = a;
	tmp.no = num++;
	(this->t_list).push_back(tmp);
	count++;
}

void telebook::output() {
	std::cout << "no" << "\t" << "name" << "\t" << "phone" << "\t" << "address" << std::endl;
	for (auto tmp : t_list) {
		std::cout << tmp.no << "\t" << tmp.name << "\t" << tmp.phone << "\t" << tmp.address << std::endl;
	}
}

void telebook::del(unsigned int no) {
	std::vector<record>::iterator it;
	for (it = (this->t_list).begin(); it < (this->t_list).end(); it++) {
		if (it->no == no) {
			(this->t_list).erase(it);
			count--;
			return;
		}
	}
	std::cout << "No such record!" << std::endl;
}

void telebook::edit(unsigned int no) {
	std::vector<record>::iterator it;
	for (it = t_list.begin(); it < t_list.end(); it++) {
		if (it->no == no) {
			std::cout << "no" << "\t" << "name" << "\t" << "phone" << "\t" << "address" << std::endl;
			std::cout << it->no << "\t" << it->name << "\t" << it->phone << "\t" << it->address << std::endl;
			break;
		}
	}
	std::cout << "name:";
	std::getline(std::cin, it->name);
	std::cout << "phone:";
	std::getline(std::cin, it->phone);
	std::cout << "address:";
	std::getline(std::cin, it->address);
	std::cout << "Completed!" << std::endl;
}

void telebook::search_name(const std::string& name) const{
	bool flag1 = true, flag2 = true;
	std::vector<record>::const_iterator it;
	for (it = t_list.begin(); it < t_list.end(); it++) {
		if (it->name == name) {
			if (flag1) {
				std::cout << "no" << "\t" << "name" << "\t" << "phone" << "\t" << "address" << std::endl;
				flag1 = false;
			}
			flag2 = false;
			std::cout << it->no << "\t" << it->name << "\t" << it->phone << "\t" << it->address << std::endl;
		}
	}
	if(flag2) std::cout << "No such record!" << std::endl;
}

void telebook::search_phone(const std::string& phone) const {
	bool flag1 = true, flag2 = true;
	std::vector<record>::const_iterator it;
	for (it = t_list.begin(); it < t_list.end(); it++) {
		if (it->phone == phone) {
			if (flag1) {
				std::cout << "no" << "\t" << "name" << "\t" << "phone" << "\t" << "address" << std::endl;
				flag1 = false;
			}
			flag2 = false;
			std::cout << it->no << "\t" << it->name << "\t" << it->phone << "\t" << it->address << std::endl;
		}
	}
	if(flag2) std::cout << "No such record!" << std::endl;
}

int cmd_shell() {
	fflush(stdin);
	std::cout << "admin:cmgr>";
	std::string tmp;
	std::getline(std::cin, tmp);
	if (tmp == "input") return 0;
	else if (tmp == "output") return 1;
	else if (tmp == "search name") return 2;
	else if (tmp == "search phone") return 3;
	else if (tmp == "edit") return 4;
	else if (tmp == "delete") return 5;
	else if (tmp == "help") return 6;
	else if (tmp == "exit") return 7;
	else return 8;
}

void welcome() {
	std::cout << "Designed by wjy_1230@126.com" << std::endl << "Type \"help\" for instruction!" << std::endl;
}

int main() {
	telebook m;
	std::string p, a, n,_name,_number;
	unsigned int no;
	int choice;
	welcome();
	do {
		choice = cmd_shell();
		switch (choice) {
		case 0:
			std::cout << "Please input the name:";
			std::getline(std::cin, n);
			std::cout << "Please input the phone number:";
			std::getline(std::cin, p);
			std::cout << "Please input the address:";
			std::getline(std::cin, a);
			m.input(p, n, a);
			std::cout << "Completed!There are"<< count << "records in total." << std::endl;
			break;
		case 1:
			m.output();
			break;
		case 2:
			std::cout << "Please input the name:";
			std::getline(std::cin, _name);
			m.search_name(_name);
			break;
		case 3:
			std::cout << "Please input the phone number:";
			std::getline(std::cin, _number);
			m.search_phone(_number);
			break;
		case 4:
			std::cout << "Please input the no:";
			std::cin >> no;
			getchar();
			m.edit(no);
			break;
		case 5:
			std::cout << "Please input the no:";
			std::cin >> no;
			m.del(no);
			std::cout << "Completed! There are "<<count<< " records in total." << std::endl;
			break;
		case 6:
			std::cout << "input; output; search name; search phone; edit; delete; help; exit;" << std::endl;
			break;
		case 7:
			break;
		default:
			break;
		}
	} while (choice != 7);
	return 0;
}
